package com.cityos.android.mystudynestedscroll.ui.behavior

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import com.cityos.android.mystudynestedscroll.R
import com.cityos.android.mystudynestedscroll.common.utils.DisplayUtils
import com.google.android.material.tabs.TabLayout
import com.youth.banner.Banner

/**
 * @author hailong
 * @date 2024年03月04日 16:54
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class UcTabLayoutBehavior(context: Context? , attrs: AttributeSet?) : CoordinatorLayout.Behavior<TabLayout>(context , attrs) {

    private val scrollMax = DisplayUtils.dp2px(context , 100f)
    private var dependencyView: View? = null


    override fun layoutDependsOn(parent: CoordinatorLayout , child: TabLayout , dependency: View): Boolean {
        return dependency.id == R.id.uc_home_banner && dependency is Banner<* , *>
    }


    override fun onDependentViewChanged(parent: CoordinatorLayout , child: TabLayout , dependency: View): Boolean {
        if (layoutDependsOn(parent , child , dependency)) {
            child.translationY = dependency.translationY * 3 / 2
            return true
        }
        return false
    }

    override fun onLayoutChild(parent: CoordinatorLayout , child: TabLayout , layoutDirection: Int): Boolean {
        val dependencies = parent.getDependencies(child)
        for (dependency in dependencies) {
            if (layoutDependsOn(parent , child , dependency)) {
                val dp2px = DisplayUtils.dp2px(parent.context , 50f)
                val bottom = dependency.bottom
                child.layout(0 , bottom , parent.width , bottom + dp2px)
                return true
            }
        }
        return super.onLayoutChild(parent , child , layoutDirection)
    }

}